<?php
namespace Home\Controller;
use Home\Controller\HomeController;
use Common\Model\MemberModel;
use Common\Api\SendPhoneCodeApi;
class LoginController extends HomeController{
     //登录
    public function checkLogin(){
	    $phone = I('phone')?I('phone'):null;
	    $password = I('password')?I('password'):null;
	    if(empty($phone)){
	       $data_return['status'] = -1;    
	       $data_return['info'] = '请输入手机号';    
           return $this->ajaxReturn($data_return);//请输入用户名
	    }
	    if(empty($password)){
	       $data_return['status'] = -2;    
	       $data_return['info'] = '请输入密码';    
           return $this->ajaxReturn($data_return);//请输入密码
	    }
	    
	    $M_USER = new MemberModel();
	    $where_getuser['phone'] = $phone;
	    $user = $M_USER->getUser($where_getuser);
	    if($user['is_lock']==1){
	        $data_return['status'] = -3;
	        $data_return['info'] = '该账号已被冻结,如有问题请联系客服'.$this->config['dongjie_kefu'];
	        return $this->ajaxReturn($data_return);
	    }
	    if(!$user){
	       $data_return['status'] = -3;    
	       $data_return['info'] = '该用户不存在';    
           return $this->ajaxReturn($data_return);//用户名不存在
	    }
	    if(md5($password)!=$user['pwd']){
	       $data_return['status'] = -4;    
	       $data_return['info'] = '密码不正确';    
          return $this->ajaxReturn($data_return);//密码错误
	    }
	   
	    //获取token
	    $where_token['uid'] = $user['member_id'];
	    $token_log = M('Token')->where($where_token)->find();
	    $token = $token_log['token'];
	    if(!$token_log){
	        $data_token['uid']   = $user['member_id'];
	        $data_token['token'] = $user['member_id'].md5(time());
	        M('Token')->add($data_token);
	        $token = $data_token['token'];
	    }
	    $new_ip = get_client_ip();
	    $old_login_ip = $user['login_ip'];
	    $data_ip['login_time']= time();
	    if($new_ip!=$old_login_ip){
	    	//如果当前操作Ip和上次不同更新登录IP以及登录时间
	    	$data_ip['login_ip'] = $new_ip;
	    }
	    $where_login['member_id'] = $user['member_id'];
	    $r = M('Member')->where($where_login)->save($data_ip);
	    $data_return['token'] = $token;
	    $data_return['status'] = 1;
	    $data_return['info'] = '登录成功';
	    $this->ajaxReturn($data_return);
    }
    //发送验证码
    public function sendPhone(){
    	$phone = I('phone')?I('phone'):null;
    	$code_name = 'zhaohui';
    	if(empty($phone)){
    		$data_return['status'] = -41;
    		$data_return['info']   = '请输入手机号';
    		return $this->ajaxReturn($data_return);
    	}
    	if(!regex($phone,'phone')){
    		$data_return['status'] = -8;
    		$data_return['info']   = '手机号格式错误';
    		return $this->ajaxReturn($data_return);
    	}
    	$SENDPHONE = new SendPhoneCodeApi($phone);
    	$send = $SENDPHONE->send($code_name);
    	if($send == 1){
    		$data_return['status'] = 1;
    		$data_return['info']   = '发送成功';
    		return $this->ajaxReturn($data_return);
    	}else{
    		$data_return['status'] = -1;
    		$data_return['info']   = $send;
    		return $this->ajaxReturn($data_return);
    	}
    }
    //忘记密码
    public function forgetPassword(){
    	$pwd   		= I('pwd');
    	$repwd      = I('repwd');
    	$phone_code = I('phone_code');
    	$phone = I('phone');
    	//判断是否为空
    	if(empty($phone)){
    		$data_return['status'] = -1;
    		$data_return['info']   = '请输入手机号';
    		return $this->ajaxReturn($data_return);
    	}
    	if(empty($phone_code)){
    		$data_return['status'] = -1;
    		$data_return['info']   = '请输入手机验证码';
    		return $this->ajaxReturn($data_return);
    	}
    	if(empty($pwd)){
    		$data_return['status'] = -3;
    		$data_return['info']   = '请输入新密码';
    		return $this->ajaxReturn($data_return);
    	}
    	if(empty($repwd)){
    		$data_return['status'] = -4;
    		$data_return['info']   = '请确认新密码';
    		return $this->ajaxReturn($data_return);
    	}
    	//判断正则
    	if(!regex($phone,'phone')){
    		$data_return['status'] = -3;
    		$data_return['info']   = '手机号格式错误';
    		return $this->ajaxReturn($data_return);
    	}
    	if(!regex($pwd,'password')){
    		$data_return['status'] = -3;
    		$data_return['info']   = '密码格式错误';
    		return $this->ajaxReturn($data_return);
    	}
    	if($phone_code != session('codezhaohui'.$phone)){
    		$data_return['status'] = -13;
    		$data_return['info']   = '手机验证码有误';
    		return $this->ajaxReturn($data_return);
    	}
    	//密码确认密码是否相等
    	if($pwd!=$repwd){
    		$data_return['status'] = -3;
    		$data_return['info']   = '两次密码不一致';
    		return $this->ajaxReturn($data_return);
    	}   
    	//查询数据库
    	$USER = new MemberModel();
    	$uid = $USER->getUserByPhone($phone)['member_id'];
    	if(!$uid){
    		$data_return['status'] = -3;
    		$data_return['info']   = '该用户不存在';
    		return $this->ajaxReturn($data_return);
    	}
    	//修改密码
    	$data['pwd'] = md5($pwd);
    	$editpwd = $USER->saveUser($data,$uid);
    	if($editpwd){
    		$data_return['status'] = 1;
    		$data_return['info']   = '密码找回成功';
    		return $this->ajaxReturn($data_return);
    	}else{
    		$data_return['status'] = 0;
    		$data_return['info']   = '操作失败';
    		return $this->ajaxReturn($data_return);
    	}
    }

}